import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class Test{
    static ArrayList<Goods> GoodsCar = new ArrayList<Goods>();
    public static void menu() {
        System.out.println("****************************");
        System.out.println("1.添加商品");
        System.out.println("2.移除商品");
        System.out.println("3.计算价格");
        System.out.println("4.打印商品信息");
        System.out.println("0.退出程序");
        System.out.println("****************************");
    }
    public static void addOperation(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入需要添加商品的名称:");
        String name = scanner.nextLine();
        System.out.println("请输入商品价格:");
        double price = scanner.nextDouble();
        System.out.println("请输入商品保质期");;
        int expiration = scanner.nextInt();
        GoodsCar.add(new Goods(name,price,expiration));
    }
    public static void removeOperation(){
        if(GoodsCar.size() == 0) {
            System.out.println("没有商品。");
            System.out.println("请添加商品后再移除。");
            return;
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入移除商品名称.");
        String name = scanner.nextLine();
        /*Iterator it =  GoodsCar.iterator();
        while(it.hasNext()) {
            Object itname = it.next();
            if(name.equals(itname)) {
                GoodsCar.remove(itname);
            }
        }*/
        boolean flg = true;
        for (int i = 0; i < GoodsCar.size(); i++) {
            if(GoodsCar.get(i).getName().equals(name)) {
                GoodsCar.remove(i);
                flg = false;
            }
        }
        if(flg != false) {
            System.out.println("没有找到这个商品，购物车不存在此商品。");
        }
    }
    public static void computeOperation(){
        if(GoodsCar.size() == 0) {
            System.out.println("购物车中没有商品。");
            System.out.println("请添加商品后再重试。");
        }
        double price = 0;
        for (int i = 0; i < GoodsCar.size(); i++) {
            price = GoodsCar.get(i).getprice() + price;
        }
        System.out.println("商品的价格一共为"+price+"元");
    }
    public static void printOperation() {
        System.out.println("正在获取商品信息。");
        for (int i = 0; i < 10; i++) {
            try{
                Thread.sleep(100);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            System.out.print("---------");
        }
        System.out.println();
        if(GoodsCar.size() == 0) {
            System.out.println("没有商品。");
            System.out.println("请添加商品后再重试。");
            return;
        }
        for (int i = 0; i < GoodsCar.size(); i++) {
            System.out.println(GoodsCar.get(i).toString());
        }
    }
    public static void main(String[] args) {

        Goods goods = null;
        int choise;
        Scanner scanner = new Scanner(System.in);
        while(true) {
            menu();
            System.out.println("请输入你的操作");
            choise = scanner.nextInt();
            if(choise == 0) {
                System.out.println("程序退出");
                break;
            }
            switch (choise) {
                case 1:
                    addOperation();
                    break;
                case 2:
                    removeOperation();
                    break;
                case 3:
                    computeOperation();
                    break;
                case 4:
                    printOperation();
                    break;
                default:
                    System.out.println("输入错误,请重新选择:");
                    break;
            }
        }
    }

}
